<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace datos;

/**
 * Description of ClienteDao
 *
 * @author Jorge
 */
class ClienteDao {
    //put your code here
    private $conexion;
    
    function __construct($conexion) {
        $this->conexion = $conexion;
    }

    
    public function getConexion() {
        return $this->conexion;
    }

    public function setConexion($conexion) {
        $this->conexion = $conexion;
    }
    
    public function buscarCliente($cliente, \model\UserSession $user){
        $cliente = '%' . $cliente . '%';
        
        if(strcasecmp($user->getPerfil(), 'user') != 0) {
            $sql = "SELECT cod_cli, 
                         nombre, 
                         rut, 
                         giro, 
                         direccion, 
                         comuna, 
                         ciudad, 
                         telefono, 
                         fax, 
                         mail, 
                         contacto, 
                         estado, 
                         saldo, 
                         cod_vendedor
                    FROM cliente
                    WHERE (cod_cli like ? OR nombre like ?)
                    AND estado != 'b'
                    ORDER BY nombre";
            $consulta = $this->conexion->prepare($sql);
            $consulta->execute(array($cliente, $cliente));
            return $consulta->fetchAll();
        }else {
            $sql = "SELECT cod_cli, 
                         nombre, 
                         rut, 
                         giro, 
                         direccion, 
                         comuna, 
                         ciudad, 
                         telefono, 
                         fax, 
                         mail, 
                         contacto, 
                         estado, 
                         saldo, 
                         cod_vendedor
                    FROM cliente
                    WHERE (cod_cli like ? OR nombre like ?)
                    AND estado!='b'
                    AND cod_vendedor = ?
                    ORDER BY nombre";   
            $consulta = $this->conexion->prepare($sql);
            $consulta->execute(array($cliente, $cliente, $user->getCodVendedor()));
            return $consulta->fetchAll();
        }                        
    }
    
    public function getClienteByRut($rutCliente){    	
		$cliente = array();		
		$sql = "CALL ClienteGetByRut( :rutCliente, @errId, @errGlosa)";
		$consulta = $this->conexion->prepare($sql);
		$consulta->bindParam(":rutCliente", $rutCliente, \PDO::PARAM_STR);
		$consulta->execute();		
		$cliente = $consulta->fetch(\PDO::FETCH_ASSOC);
		$consulta->closeCursor();
		$sql = "select 	@errId 		as errId
						,@errGlosa 	as errGlosa";
		$outParam = $this->conexion->query($sql)->fetch(\PDO::FETCH_ASSOC);
		$cliente['errId'] = $outParam['errId'];
		$cliente['errGlosa'] = $outParam['errGlosa'];
		return $cliente;
		
    }


}
